// pages/personD/personD.js
const app = getApp();
const db = wx.cloud.database();
// 投递人员详情
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isShowMask: false,
    deliveryPersonResumeInfo: {},
    userInfo: {},
    getHiredSure: false,

    inviteInfo: {
      thing1: {
        value: ""
      },
      thing2: {
        value: ""
      },
      time4: {
        value: ""
      },
      thing5: {
        value: ""
      }
    },
  },
  // 刷新面试邀请信息
  inputChange(e) {
    this.setData({
      ['inviteInfo.' + e.target.id + '.value']: e.detail.value
    });
    // console.log(this.data.inviteInfo);
  },
  // 收回
  withdraw() {
    var that = this
    that.setData({
      isShowMask: false
    })
  },
  // 点击邀请面试
  getInvate() {
    var that = this

    if (this.data.deliveryPersonResumeInfo.status == "待查看") {
      that.setData({
        isShowMask: true
      });
    } else {
      wx.showToast({
        icon: "none",
        title: '您' + that.data.deliveryPersonResumeInfo.status + '该人员',
      })
    }
  },
  // 确定邀请
  invateSure() {
    var that = this;
    const templateId = 'ZsVNF1tKYrk_isoYXtWlj2UCpAqAdlHYfDMLOTihRhY' //模板ID
    // 查询订阅消息表
    db.collection('subscribeRecord').where({
      _openid: that.data.userInfo._openid,
      status: 0,
      templateId: templateId
    }).limit(1).get({
      success: res => {
        if (res.data.length != 0) {
          console.log(res.data[0]);
          const id = res.data[0]._id; //简写一下该条记录的id
          //本地修改返回来的记录的subMsg
          res.data[0].subMsg = that.data.inviteInfo;
          // 调用云函数发送订阅消息通知
          wx.cloud.callFunction({
            name: "subscribe",
            data: res.data[0],
            success: res2 => {
              console.log(res2);
              if (res2.result.errCode == 47003) {
                wx.showModal({
                  content: "时间填写不规范，请以'yyyy-mm-dd hh-mm'的形式填写",
                  showCancel: false,
                })
                console.log(res2.result.errMsg);
                return
              } else if (res2.result.errCode == 43101) {
                console.log('用户拒绝接受消息，如果用户之前曾经订阅过，则表示用户取消了订阅关系')
                return
              } else if (res2.result.errCode == 41030) {
                console.log('page路径不正确，需要保证在现网版本小程序中存在，与 app.json 保持一致')
                return
              } else {
                //本地数据刷新
                app.navTmpInfo = that.data.deliveryPersonResumeInfo;
                that.setData({
                  ['deliveryPersonResumeInfo.status']: '已邀面'
                })
                //修改订阅记录状态和发送信息
                db.collection('subscribeRecord').doc(id).update({
                  data: {
                    subMsg: that.data.inviteInfo,
                    status: 1
                  },
                  success: res3 => {
                    console.log("subscribeRecord表修改成功")
                  },
                  fail: res3 => {
                    console.log(res3);
                  }
                });

                // 修改简历记录状态
                db.collection('resume').where({
                  job_id: that.data.deliveryPersonResumeInfo.job_id,
                  _openid: that.data.deliveryPersonResumeInfo._openid,
                  timestamp: that.data.deliveryPersonResumeInfo.timestamp
                }).update({
                  data: {
                    status: "已邀面"
                  },
                  success: function (res) {
                    that.setData({
                      isShowMask: false
                    })
                    wx.showToast({
                      title: "邀请成功",
                      icon: 'success',
                      mask: true,
                    })
                    console.log("resume表修改成功")
                  },
                  fail: function () {
                    console.log("resume未修改成功");
                  }
                })
              }

            },
            fail: res => {
              console.log(res);
              wx.showToast({
                icon: 'none',
                title: '云函数调用失败'
              })
            }
          })
        } else {
          console.log("订阅表中没有此记录");
          wx.showToast({
            icon: "none",
            title: '数据异常',
          })
        }
      },
      fail: err => {
        console.log(err);
        wx.showToast({
          title: '查询订阅表失败',
        })
      }
    });

  },
  // getHired
  getHired() {
    var that = this;
    wx.showModal({
      title: '提示',
      content: '确定录用该学僧',
      success(res) {
        if (res.confirm) {
          wx.showLoading({
            title: '请求中',
          })
          console.log('用户点击确定');
          that.setData({
            getHiredSure: true
          });
          db.collection('resume').where({
            _openid: that.data.deliveryPersonResumeInfo._openid,
            job_id: that.data.deliveryPersonResumeInfo.job_id,
            timestamp: that.data.deliveryPersonResumeInfo.timestamp
          }).update({
            data: {
              status: '已录用'
            },
            success: function (res) {
              wx.showToast({
                title: '已录用',
              });
            },
            fail: function () {
              console.log("录用失败");
            },
            complete: function () {
              wx.hideLoading()
            }
          })

        } else if (res.cancel) {
          console.log('用户点击取消');
        }
      }
    })
  },
  getRefuse() {
    var that = this;
    db.collection('resume').where({
      _openid: that.data.deliveryPersonResumeInfo._openid,
      job_id: that.data.deliveryPersonResumeInfo.job_id,
      timestamp: that.data.deliveryPersonResumeInfo.timestamp
    }).update({
      data: {
        status: '已拒绝'
      },
      success: function (res) {
        that.setData({
          isShowMask: false
        })
        wx.showToast({
          title: "已拒绝",
          icon: 'success',
          duration: 1000
        })
      },
      fail: function () {
        console.log("拒绝失败");
      }
    })

  },
  // 预览简历
  previewResume() {
    let that = this;
    wx.cloud.getTempFileURL({
      fileList: [that.data.deliveryPersonResumeInfo.fileID],
      success: res => {
        // console.log(res);
        // console.log(res.fileList[0].tempFileURL);
        wx.showLoading({
          title: '加载中',
        })
        wx.downloadFile({
          url: res.fileList[0].tempFileURL,
          success: res => {
            // console.log(res)
            wx.openDocument({
              filePath: res.tempFilePath,
              success: res => {
                console.log('预览成功');
              },
              fail: res => {
                console.log('预览失败');
              }
            })
          },
          fail: res => {
            wx.showToast({
              title: '网络错误',
              icon: "error"
            })
          },
          complete: () => {
            wx.hideLoading();
          }
        })

      }
    });
  },


  getBackDperson() {
    wx.navigateBack({
      delta: 1
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let that = this;
    this.setData({
      deliveryPersonResumeInfo: app.navTmpInfo
    })
    console.log(this.data.deliveryPersonResumeInfo);

    // 查用户表来获取该人员信息
    db.collection('users').where({
      _openid: that.data.deliveryPersonResumeInfo._openid
    }).get({
      success: res => {
        if (res.data.length != 0) {
          that.setData({
            userInfo: res.data[0]
          });
          that.setData({
            ['inviteInfo.thing1.value']: this.data.userInfo.name
          })
        }
      },
      fail: res => {
        console.log(res);
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    console.log(this.data.deliveryPersonResumeInfo.status);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})